// pages/goods_list/goods_list.js
var app = getApp();
var url = app.globalData.url;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    search_q:'',
    class_id:'',
    sort_type:'综合排序',
    URL: app.globalData.url,
    brand_id:0,//品牌
    spec:0,//规格
    attr:'',//属性
    sort:'is_hot',//默认热卖排序
    sort_asc:'desc',//默认降序排序
    price:'',//价钱
    start_price:0,//输入框开始价钱
    end_price: 0,//输入框结束价钱
    goods_list:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '商品列表'
    })
    console.log(options.keyword);
    //默认商品列表
    var that = this;
    if (options.keyword){
      wx.request({
        url: url + "/api/shop/goods_search_list",
        data: {
          q: options.keyword,
          // sort: 'is_hot',
          // sort_asc: 'desc',
          // id: e.currentTarget.id,
        },
        success: function (res) {
          // console.log(res);
          that.setData({
            goods_list: res.data.goods_list,
            search_q: options.keyword,
            sort_type: '综合排序',
          });
        }
      })
    }else{
      wx.request({
        url: url + "/api/shop/goods_list",
        method: "POST",
        data: {
          // id: 12,
          id: options.id,
        },
        success: function (res) {
          console.log(res);
          that.setData({
            goods_list: res.data.goods_list,
            class_id: options.id,
          });
        }
      })
    }

  },
  /**
   * 调用全局返回函数 
   */
  gotoIndex: function () {
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  /**
   * 综合排序菜单
   */
  toSort:function(e){
    console.log(e.currentTarget);
    var that = this;
      wx.showActionSheet({
          itemList: ['综合排序','价格由高到低', '价格由低到高', '最新上架时间'],
          success: function (res) {
              console.log(res.tapIndex);
              var sort_type = '';
              var sort = ''; 
              var sort_t = '';
              if (res.tapIndex === 0){
                sort_type = 'is_hot';
                sort = 'desc';
                sort_t = '综合排序';
              }else if (res.tapIndex === 1){
                sort_type = 'shop_price';
                sort = 'desc';
                sort_t = '价格由高到低';
              } else if (res.tapIndex === 2){
                sort_type = 'shop_price';
                sort = 'asc';
                sort_t = '价格由低到高';
              } else if (res.tapIndex === 3){
                sort_type = 'on_time';
                sort = 'desc';
                sort_t = '最新上架时间';
              } else if (res.tapIndex == undefined) {
                sort_t = e.currentTarget.dataset.current;
                that.setData({
                  sort_type: sort_t,
                });
                return false;
              }
              console.log(sort_t);
              if (e.currentTarget.dataset.q != ''){
                wx.request({
                  url: url + "/api/shop/goods_search_list",
                  data: {
                    q: e.currentTarget.dataset.q,
                    sort: sort_type,
                    sort_asc: sort,
                  },
                  success: function (res) {
                    console.log(res);
                    that.setData({
                      goods_list: res.data.goods_list,
                      sort_type: sort_t,
                    });
                  }
                })
              }else{
                wx.request({
                  url: url + "/api/shop/goods_list",
                  method: "POST",
                  data: {
                    sort:sort_type,
                    sort_asc:sort,
                    // id: 12,
                    id: e.currentTarget.id,
                  },
                  success: function (res) {
                    console.log(res);
                    that.setData({
                      goods_list: res.data.goods_list,
                      sort_type: sort_t,
                    });
                  }
                })
              }

          },
          fail: function (res) {
              // console.log(res)
          }
      })
  },
  searchsubmit:function(i){
    console.log(i.detail.value.search);
    var that = this;
    wx.request({
      url: url + "/api/shop/goods_search_list",
      data: {
        q: i.detail.value.search,
        // sort: 'is_hot',
        // sort_asc: 'desc',
        // id: e.currentTarget.id,
      },
      success: function (res) {
        console.log(res);
        that.setData({
          goods_list: res.data.goods_list,
          search_q: i.detail.value.search,
          sort_type: '综合排序',
        });
      }
    })
  },
  toSale:function(e){
    console.log(e);
    var that = this;
    if (e.currentTarget.dataset.q != '') {
      wx.request({
        url: url + "/api/shop/goods_search_list",
        data: {
          q: e.currentTarget.dataset.q,
          sort: 'sales_sum',
          sort_asc: 'desc',
        },
        success: function (res) {
          console.log(res);
          that.setData({
            goods_list: res.data.goods_list,
          });
        }
      })
    }else{
      wx.request({
        url: url + "/api/shop/goods_list",
        method: "POST",
        data: {
          sort: 'sales_sum',
          sort_asc: 'desc',
          // id: 12,
          id: e.currentTarget.id,
        },
        success: function (res) {
          console.log(res);
          that.setData({
            goods_list: res.data.goods_list,
          });
        }
      })
    }
  },
  goGoodsInfo:function(b){
    console.log(b);
    wx.navigateTo({
      url: '/pages/goodsInfo/goodsInfo?id=' + b.currentTarget.id,
    })
  }
})